package cn.fancy.sdk.login.view;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.fancy.sdk.baseview.BaseView;
import cn.fancy.sdk.baseview.IActivityManager;
import cn.fancy.sdk.common.utils.ResUtil;
import cn.fancy.sdk.utils.CommMessage;
import java.util.ArrayList;

public class AccountListViewNew extends BaseView {
    
    private Context mContext;
    private Activity ownerActivity;
    private IActivityManager activityMgr;
    private TextView list_item_null;
    private ListView list_item;
    private ImageView guild_back;
    private String phonenum;
    
    ArrayList<String> idList;
    ArrayList<String> nameList;
    
    public AccountListViewNew(Activity cActivity, IActivityManager activityMgr, String uphone, ArrayList<String> idList, ArrayList<String> nameList) {
        super(cActivity.getBaseContext(), ResUtil.getLayoutId(cActivity.getBaseContext(), "fancy_acclist"));
        mContext = cActivity.getBaseContext();
        this.activityMgr = activityMgr;
        ownerActivity = cActivity;
        
        initView();
        this.idList = idList;
        this.nameList = nameList;
        
        PhoneListAdapter phoneListAdapter = new PhoneListAdapter(ownerActivity, ResUtil.getLayoutId(mContext, "guild_acclistadapter"), nameList);
        list_item.setAdapter(phoneListAdapter);
        phonenum = uphone;
    }
    
    private void initView() {
        list_item_null = (TextView) findViewById(ResUtil.getId(mContext,
            "list_item_null"));
        list_item_null.setVisibility(View.INVISIBLE);
        
        list_item = (ListView) findViewById(ResUtil
            .getId(mContext, "list_item"));
        guild_back = (ImageView) findViewById(ResUtil.getId(mContext,
            "guild_back"));
        
        list_item.setVisibility(View.VISIBLE);
        list_item_null.setVisibility(View.INVISIBLE);
        guild_back.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        if (v.getId() == ResUtil.getId(mContext, "guild_back")) {
            activityMgr.NewpopViewFromStack();
        }
    }
    
    private class PhoneListAdapter extends ArrayAdapter<String> {
        
        PhoneListAdapter(Activity ownerActivity, int layout, ArrayList<String> nameList) {
            super(ownerActivity, ResUtil.getLayoutId(mContext, "guild_acclistadapter"), nameList);
        }
        
        public View getView(final int position, View convertView, ViewGroup parent) {
            View row;
            if (convertView == null) {
                LayoutInflater inflater = ownerActivity.getLayoutInflater();
                row = inflater.inflate(ResUtil.getLayoutId(mContext, "guild_acclistadapter"), parent, false);
            } else {
                row = convertView;
            }
            final TextView tv = (TextView) row.findViewById(ResUtil.getId(mContext, "phonetext"));
            tv.setText(getItem(position));
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    CommMessage.sFindUser = tv.getText().toString();
                    activityMgr.NewpopViewFromStack();
                    activityMgr.pushViewToStack(new ResetPassView(ownerActivity,
                        activityMgr, phonenum, idList.get(position), getItem(position)));
                }
            });
            return row;
        }
    }
    
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
}
